package com.lianniu.mgr.supplyManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.supplyManage.dao.ISupplyCommissionDao;
import com.lianniu.mgr.supplyManage.service.ISupplyCommissionService;
import com.lianniu.mgr.supplyManage.vo.CommissionConditionVo;
import com.lianniu.mgr.supplyManage.vo.SupplyCommissionVo;
import com.lianniu.mgr.util.StringUtil;
@Service
public class SupplyCommissionServiceImpl implements ISupplyCommissionService{

  private static Logger logger = LoggerFactory.getLogger(SupplyCommissionServiceImpl.class);
  @Autowired
  ISupplyCommissionDao commissionDao;
  @Override
  public BaseResult getCommissionListForPage(CommissionConditionVo condition,
      PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    logger.info(String.format("获取供应商点位列表，condition＝[%s],pagination=[%s]",
    		JSON.toJSONString(condition),JSON.toJSONString(paginationInfo)));
    List<SupplyCommissionVo> commissionList = commissionDao.getCommissionListForPage(condition);
    logger.info("获取到的点位列表为："+JSON.toJSONString(commissionList));
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", commissionList);

    return BaseResult.success(retmap);
  }
@Override
public BaseResult getCommission(String supplyId) {
	if(StringUtil.isEmpty(supplyId)){
		return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	}
	logger.info(String.format("获取id＝[%s]的佣金点位",supplyId));
	SupplyCommissionVo commission = commissionDao.getCommission(supplyId);
	logger.info("获取的佣金点位为："+JSON.toJSONString(commission));
	return BaseResult.success(commission);
}
@Override
public BaseResult deleteCommission(String ids) {
  if(StringUtil.isEmpty(ids)){
    return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
  }
  String[] idArr = ids.split(",");
  for(String id : idArr){
    commissionDao.deleteCommission(id);
  }
  return BaseResult.success();
}
@Override
public BaseResult deleteSupplyCommission(String supplyId) {
  commissionDao.deleteSupplyCommission(supplyId);
  return BaseResult.success();
}
@Override
public BaseResult editCommission(SupplyCommissionVo commission) {
  deleteSupplyCommission(commission.getSupplyID());
  commissionDao.editCommission(commission);
  return BaseResult.success();
}

}
